﻿package com.game.view.layer
{
	import com.dusk.util.FilterUtil;
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.manager.NotificationManager;
	import com.game.manager.SoundManager;
	import com.game.manager.SettingManager;
	
	import fl.controls.Button;
	
	import fl.controls.ComboBox;
	import fl.controls.Slider;
	import fl.controls.TextInput;
	import fl.events.SliderEvent;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class GameSetting extends LayerBase
	{
		public function GameSetting():void
		{
			super();
		}
		
		public var bgmStay:TextField;
		public var difficulty:ComboBox;
		public var numStay:TextField;
		public var numStyle:ComboBox;
		public var saveBtn:Button;
		public var skillStay:TextField;
		public var sndBar:Slider;
		public var sndTxt:TextInput;
		public var closeBtn:SimpleButton;
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("GameSetting") as MovieClip;
			TextUtil.redrawTxt(mc);
			bgmStay = mc.getChildByName("bgmStay") as TextField;
			numStay = mc.getChildByName("numStay") as TextField;
			skillStay = mc.getChildByName("skillStay") as TextField;
			closeBtn = mc.getChildByName("closeBtn") as SimpleButton;
			difficulty = mc.getChildByName("difficulty") as ComboBox;
			numStyle = mc.getChildByName("numStyle") as ComboBox;
			saveBtn = mc.getChildByName("saveBtn") as Button;
			sndTxt = mc.getChildByName("sndTxt") as TextInput;
			sndBar = mc.getChildByName("sndBar") as Slider;
			setCloseBtn(closeBtn);
			addChild(mc);
			refreshInfo();
		}
		
		public function refreshInfo():void
		{
			if (SoundManager.bgmStay)
			{
				bgmStay.text = "开 启";
			}
			else
			{
				bgmStay.text = "关 闭";
			}
			
			if (SoundManager.skillStay)
			{
				skillStay.text = "开 启";
			}
			else
			{
				skillStay.text = "关 闭";
			}
			
			if (gc.showHurtNum)
			{
				numStay.text = "开 启";
			}
			else
			{
				numStay.text = "关 闭";
			}
			difficulty.selectedItem = difficulty.getItemAt(gc.difficulty);
			numStyle.selectedItem = numStyle.getItemAt(gc.userData.hurtNumStyle);
			sndBar.value = SoundManager.currentVolume;
			sndTxt.text = sndBar.value.toString();
		}
		
		override protected function onAdded():void
		{
			bgmStay.addEventListener(MouseEvent.CLICK, react);
			bgmStay.addEventListener(MouseEvent.ROLL_OVER, react);
			bgmStay.addEventListener(MouseEvent.ROLL_OUT, react);
			skillStay.addEventListener(MouseEvent.CLICK, react);
			skillStay.addEventListener(MouseEvent.ROLL_OVER, react);
			skillStay.addEventListener(MouseEvent.ROLL_OUT, react);
			numStay.addEventListener(MouseEvent.CLICK, react);
			numStay.addEventListener(MouseEvent.ROLL_OVER, react);
			numStay.addEventListener(MouseEvent.ROLL_OUT, react);
			sndBar.addEventListener(SliderEvent.CHANGE, onSndBarChange);
			sndTxt.addEventListener(Event.CHANGE, onSndTxtChange);
			saveBtn.addEventListener(MouseEvent.CLICK, saveAllSetting);
		}
		
		override protected function onRemoved():void
		{
			bgmStay.removeEventListener(MouseEvent.CLICK, react);
			bgmStay.removeEventListener(MouseEvent.ROLL_OVER, react);
			bgmStay.removeEventListener(MouseEvent.ROLL_OUT, react);
			skillStay.removeEventListener(MouseEvent.CLICK, react);
			skillStay.removeEventListener(MouseEvent.ROLL_OVER, react);
			skillStay.removeEventListener(MouseEvent.ROLL_OUT, react);
			numStay.removeEventListener(MouseEvent.CLICK, react);
			numStay.removeEventListener(MouseEvent.ROLL_OVER, react);
			numStay.removeEventListener(MouseEvent.ROLL_OUT, react);
			sndBar.removeEventListener(SliderEvent.CHANGE, onSndBarChange);
			sndTxt.removeEventListener(Event.CHANGE, onSndTxtChange);
			saveBtn.removeEventListener(MouseEvent.CLICK, saveAllSetting);
		}
		
		private function onSndBarChange(evt:SliderEvent):void
		{
			sndTxt.text = evt.value.toString();
		}
		
		private function onSndTxtChange(evt:Event):void
		{
			if(evt.currentTarget.text == "")
			{
				return;
			}
			var value:int = int(evt.currentTarget.text);
			if(value > 100)
			{
				value = 100;
			}
			evt.currentTarget.text = value.toString();
			sndBar.value = value;
		}
		
		private function react(evt:MouseEvent):void
		{
			var tf:TextFormat;
			switch (evt.type)
			{
				case MouseEvent.CLICK:
					if(evt.currentTarget.text == "开 启")
					{
						evt.currentTarget.text = "关 闭"
					}
					else
					{
						evt.currentTarget.text = "开 启"
					}
					break;
				case MouseEvent.ROLL_OVER:
					tf = evt.currentTarget.defaultTextFormat;
					tf.color = 0x00FFFF;
					evt.currentTarget.setTextFormat(tf);
					evt.currentTarget.defaultTextFormat = tf;
					FilterUtil.setGlow(evt.currentTarget,0x00FFFF,0.75,5,5,2,3);
					FilterUtil.clearCache();
					break;
				case MouseEvent.ROLL_OUT:
					tf = evt.currentTarget.defaultTextFormat;
					tf.color = 0;
					evt.currentTarget.setTextFormat(tf);
					evt.currentTarget.defaultTextFormat = tf;
					FilterUtil.deleteAllFilters(evt.currentTarget);
					break;
			}
		}
		
		private function saveAllSetting(evt:MouseEvent):void
		{
			SoundManager.bgmStay = bgmStay.text == "开 启";
			SoundManager.skillStay = skillStay.text == "开 启";
			gc.showHurtNum = numStay.text == "开 启";
			gc.difficulty = int(difficulty.value);
			gc.userData.hurtNumStyle = int(numStyle.value);
			//////////
			//以下是开启游戏时的设置
			SettingManager.getSettingVO().defaultVolume = sndBar.value;
			SoundManager.setVolume(sndBar.value);
			SettingManager.saveGameSetting();
			NotificationManager.alert("保存成功");
			removeFromParent();
		}
	}
}